package com.openlogic.common.datasource.audit;

import java.util.Optional;

import com.mybatisflex.core.audit.AuditMessage;
import com.mybatisflex.core.audit.MessageFactory;
import com.openlogic.common.core.utils.HardWareUtil;
import com.openlogic.common.core.web.domain.model.LoginUser;
import com.openlogic.common.security.utils.SecurityUtil;

/**
 * @ClassName: AuditMessageFactory
 * @Description: sql审计工厂，设置 message 的基础内容，包括 platform、module、url、user、userIp、hostIp 内容
 * @author weiyongpeng
 * @date 2025年1月19日 上午8:50:28
 */
public class AuditMessageFactory implements MessageFactory {

	@Override
	public AuditMessage create() {
		LoginUser loginUser = SecurityUtil.getLoginUser();
		AuditMessage message = new AuditMessage();
		if (Optional.ofNullable(loginUser).isPresent()) {
			message.setUserIp(loginUser.getIpaddr());
			message.setUser(loginUser.getUsername());
		}else {
			message.setUser("system");
		}
		message.setHostIp(HardWareUtil.getSystemInfo().getComputerIp());
		message.setPlatform(HardWareUtil.getSystemInfo().getOsName());
		return message;
	}

}


